﻿using System.Text;
using System.Windows.Forms;

namespace QAToolsAddin.GenericDialogs.Fields {

    public class FieldRadioVertical : Field, IField {
        readonly Panel _control;

        public FieldRadioVertical(string name, string info, string[] choices, int selected, bool retore = false)
            : base(name, info, retore) {
            _control = new Panel();
            const int top = 4;
            for (int i = 0; i < choices.Length; i++) {
                _control.Controls.Add(new RadioButton {
                    Top = top + (i * 17),
                    AutoSize = true,
                    Checked = i == selected,
                    Text = choices[i],
                });
            }
            _control.Height = choices.Length * 17 + 8;
        }

        public override Control Control {
            get { return _control; }
        }

        public int Value {
            get {
                int i = -1;
                foreach(RadioButton rb in _control.Controls) {
                    ++i;
                    if (rb.Checked) 
                        return i;
                }
                return -1;
            }
            set {
                ((RadioButton)_control.Controls[value]).Checked = true;
            }
        }

        object IField.Value {
            get { return Value; }
            set { Value = (int)value; }
        }

        public override string ToDebugString() {
            var sb = new StringBuilder(base.Label + ":");
            foreach (RadioButton bt in _control.Controls)
                sb.Append(" " + bt.Text + "=" + bt.Checked.ToString());
            return sb.ToString();
        }
    }

}